package com.ld.customer.dao;

import com.ld.customer.entity.Customer;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao {

    private static Connection con;
    private static CustomerDao cd;

    private CustomerDao() {

        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/customer", "root", "root");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static CustomerDao getCd() {
        cd = new CustomerDao();
        return cd;
    }

    public List<Customer> selectAll() {
        ArrayList<Customer> list = new ArrayList<>();
        String sql = "select * from customer";
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Customer(rs.getInt("id"), rs.getString("name"),
                        rs.getString("gender"), rs.getInt("age"),
                        rs.getString("phone"), rs.getString("email")));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public boolean insert(Customer cu) {
        boolean flag = true;
        String sql = "insert into customer(id,name,gender ,age,phone, email) values(?,?,?,?,?,?)";
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, cu.getId());
            ps.setString(2, cu.getName());
            ps.setString(3, cu.getGender());
            ps.setInt(4, cu.getAge());
            ps.setString(5, cu.getPhone());
            ps.setString(6, cu.getEmail());
            int i = ps.executeUpdate();
            if (i != 1) {
                System.out.println("插入失败");
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean delete(int id) {
        boolean flag = true;
        String sql = "delete from customer where id = ?";
        PreparedStatement ps;
        try {
            ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            int i = ps.executeUpdate();
            if (i != 1) {
                System.out.println("删除失败");
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean update(Customer cu, int id) {
        boolean flag = true;
        String sql = "update customer set name = ?, gender = ?, age = ?,phone = ?, email = ? where id = ?";
        PreparedStatement ps;
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, cu.getName());
            ps.setString(2, cu.getGender());
            ps.setInt(3, cu.getAge());
            ps.setString(4, cu.getPhone());
            ps.setString(5, cu.getEmail());
            ps.setInt(6, id);
            int i = ps.executeUpdate();
            if (i != 1) {
                System.out.println("更新失败！！！");
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;

    }

    public Customer selectById(int id) {
        String sql = "select * from customer where id = ?";
        Customer cu = null;
        try {
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                cu = new Customer(rs.getString("name"), rs.getString("gender"),
                        rs.getInt("age"), rs.getString("phone"),
                        rs.getString("email"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return cu;
    }

    public void closeSource(ResultSet rs, PreparedStatement ps, Connection con) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
